//
//  CustomReplenView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/17.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "CustomReplenView.h"

@interface CustomReplenView()<UIGestureRecognizerDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) UIView *customInputV;

@property (nonatomic,strong) UILabel *topTitleLB;
///充值金额
@property (nonatomic,strong) UITextField *amount_TF;

@property (nonatomic,strong) UIButton *confirmBtn;


@property (nonatomic,assign) BOOL isHaveDian;

@end

@implementation CustomReplenView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = Color_Clear;
        [self initUI];
    }
    return self;
}
#pragma mark - UI
-(void)initUI{
    //点击背景隐藏
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    [self addSubview:self.bgView];
    
    [self.bgView addSubview:self.customInputV];
    
    [self.customInputV addSubview:self.topTitleLB];
    [self.customInputV addSubview:self.amount_TF];
    [self.customInputV addSubview:self.confirmBtn];
}
#pragma mark - lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        self.bgView.backgroundColor = RGBA(0, 0, 0, 0.3f);
    }
    return _bgView;
}
- (UIView *)customInputV{
    if (!_customInputV) {
        _customInputV = [[UIView alloc] initWithFrame:CGRectMake(15, ScreenHeight/2-120, ScreenWidth-30, 240)];
        _customInputV.clipsToBounds = YES;
        _customInputV.layer.cornerRadius = 10;
        _customInputV.backgroundColor = UIColor.whiteColor;
    }
    return _customInputV;
}
- (UILabel *)topTitleLB{
    if (!_topTitleLB) {
        _topTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(self.customInputV.width/2-50, 20, 100, 20)];
        _topTitleLB.text = @"充值";
        _topTitleLB.font = FontSet(16);
        _topTitleLB.textColor = MainGrayTextColor;
        _topTitleLB.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLB;
}
- (UITextField *)amount_TF{
    if (!_amount_TF) {
        _amount_TF = [[UITextField alloc] initWithFrame:CGRectMake(15, self.topTitleLB.bottom+30, self.customInputV.width-30, 50)];
        _amount_TF.textAlignment = NSTextAlignmentLeft;
        _amount_TF.font = FontSetWithWeight(20, 0.3f);
        _amount_TF.textColor = MainPureColor(0);
        _amount_TF.keyboardType = UIKeyboardTypeDecimalPad;
        _amount_TF.placeholder = @"  请输入充值金额";
        _amount_TF.delegate = self;
        _amount_TF.clipsToBounds = YES;
        _amount_TF.layer.cornerRadius = 5;
        _amount_TF.layer.borderWidth = 1;
        _amount_TF.layer.borderColor = MainPureColor(220).CGColor;
    }
    return _amount_TF;
}
- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmBtn.frame = CGRectMake(self.customInputV.width/2-70, self.customInputV.height - 60, 120, 40);
        _confirmBtn.clipsToBounds = YES;
        _confirmBtn.layer.cornerRadius = 20;
        _confirmBtn.backgroundColor = MainThemeColor;
        [_confirmBtn.titleLabel setFont:FontSet(17)];
        [_confirmBtn setTitle:@"确认充值" forState:normal];
        [_confirmBtn setTitleColor:UIColor.whiteColor forState:normal];
        [_confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}
#pragma mark - SEL
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.25f animations:^{
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    }];
}
- (void)dismiss {
    [UIView animateWithDuration:0.25f animations:^{
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
-(void)confirmClick{
    if ([self.amount_TF.text floatValue] <= 0) {
        [[XNProgressHUD shared] showWithTitle:@"请输入要充值的金额"];
        return;
    }
    if (self.confirmReplenBlock) {
        self.confirmReplenBlock(self.amount_TF.text);
    }
    [self dismiss];
}
#pragma mark - UITextFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // 判断是否有小数点
    if([textField.text containsString:@"."]) {
        self.isHaveDian = YES;
    }else{
        self.isHaveDian=NO;
    }
    if(string.length>0) {
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        // 不能输入.0-9以外的字符
        if(!((single >='0'&& single <='9') || single =='.')){
            return NO;
        }
        // 只能有一个小数点
        if(self.isHaveDian && single =='.'){
            return NO;
        }
        // 如果第一位是.则前面加上0.
        if((textField.text.length==0) && (single =='.')) {
            textField.text = @"0";
        }
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if([textField.text hasPrefix:@"0"]) {
            if(textField.text.length>1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1,1)];
                if(![secondStr isEqualToString:@"."]){
                    return NO;
                }
            }else{
                if(![string isEqualToString:@"."]){
                    return NO;
                }
            }
        }
        // 小数点后最多能输入两位
        if(self.isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            // 由于range.location是NSUInteger类型的，所以这里不能通过(range.location - ran.location)>2来判断
            if(range.location > ran.location) {
                if([textField.text pathExtension].length>1){
                    return NO;
                }
            }
        }
    }
    return YES;
}


#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if( [touch.view isDescendantOfView:self.customInputV]) {
        return NO;
    }
    return YES;
}







@end
